package entity

import (
	"fmt"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

//ShConfIndiDecdCpql
type ShConfIndiDecdCpql struct {
	ID   int    `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	CPBH string `gorm:"column:CPBH"`
	CPNM string `gorm:"column:CPNM"`
}

//TableName
func (ShConfIndiDecdCpql) TableName() string {
	return "SH_CONF_INDI_DECD_CPQL"
}
func (p *ShConfIndiDecdCpql) BeforeCreate(tx *gorm.DB) (err error) {
	var cpbh *string
	newTx := base.CHDB.Begin()
	err = newTx.Raw(`SELECT DISTINCT CORE_PROD_CD  FROM T_CM_RETAIL_CUST_DPSIT_ACCT_SUM_M where CORE_PROD_CD = ?`, p.CPBH).Row().Scan(&cpbh)
	if cpbh == nil {
		err = newTx.Raw(`SELECT DISTINCT ISSUE_PROD_NUM FROM  T_CM_RETAIL_STRU_DPSIT_SUM_M where ISSUE_PROD_NUM = ?`, p.CPBH).Row().Scan(&cpbh)
		if cpbh == nil {
			return fmt.Errorf("产品编号%s在系统中不存在", p.CPBH)
		}
	}
	return
}

func ShConfIndiDecdCpqlEF() base.EntityFunc {
	return func() interface{} {
		entity := ShConfIndiDecdCpql{}
		return &entity
	}
}
func ShConfIndiDecdCpqlESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ShConfIndiDecdCpql
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("ShConfIndiDecdCpql", base.NewDefaultDao(ShConfIndiDecdCpqlEF(), ShConfIndiDecdCpqlESF(), nil, "my"))
}
